package gui;
import java.awt.Dimension;
import java.util.ArrayList;

import classifier.*;

public class BoomNaarTekeningParser {
	private ArrayList<NodeMetCoordinaten> coordinates;
	private Dimension screen;
	private int nodeWidth, nodeHeight, x, level = 1, nodePercentage = 2;
	private String[] yn = new String[2];
	/*to be able to have 32 nodes next to each other the nodeWidth should be 2.083%
	 * dan houd je maximaal een halve node afstand tussen de nodes op de onderste rij.
	 * op 1024 is dat +/-20px! hah
	*/
	public BoomNaarTekeningParser(Dimension s) {
		screen = s;
		x = (int)(screen.getWidth()/2) - (nodeWidth/2);
		nodeWidth = (((int)screen.getWidth())/100)*nodePercentage;
		nodeHeight = nodeWidth/2;
		coordinates = new ArrayList<NodeMetCoordinaten>();
		yn[0] = "no"; yn[1] = "yes";
	}
	
	public void generateCoordinates(Node current, int rootX, int[] pappa) {
			int[] c = new int[2];		
			c[0] = x; c[1] = calcY();
			
			NodeMetCoordinaten n = 
					new NodeMetCoordinaten(current, c);
			n.setPappa(pappa);
			
			coordinates.add(n);
			if (pappa == null)
				pappa =  new int[2];
			pappa = c.clone();

			
			if (rootX == -1)
				rootX = x;
			if (!current.isLeaf()) {
				
				level++;			
				
				for(int i = 0;i<2;i++) {
					x=calcX(i,rootX);
					generateCoordinates(current.follow(yn[i]), x, pappa);
				}
				
				level--;
				
			} 
	}
	
	public ArrayList<NodeMetCoordinaten> getPreorderCoordinates() {
		return coordinates;
	}
	
	
	private int calcX(int leftOrRight, int rX) {
		int output;
		int step = ((int)screen.getWidth()) / (macht(2,level));
		step*=level-1;
		if (leftOrRight==0) 
			output = rX - step;
		else 			
			output = rX + step;

		
		return output;
	}
	
	private int calcY() {
		return (level*(nodeHeight*8));
	}
	
	//Deze rekenikdanook nog wel ff uitvoordetekenaar
	public int getNodeHeight() {
		return nodeHeight;
	}
	public int getNodeWidth() {
		return nodeWidth;
	}
	
	private int macht(int dit, int totdemacht) {
		int output = dit;
		if (totdemacht==0)
			return 1;
		for(int i = 1; i <= totdemacht; i++) {
			output *= i;		
		}
		return output;
	}
}
